#include "piece_car.hpp"

bool Piece_Car::move(const Coordinate &coor_end) {
    Coordinate det = coor - coor_end;
    if (det.x == 0 ^ det.y == 0) {
        //纵向路径上是否存在棋子的判断
        if (!det.y) {
            int max = std::max(coor.x, coor_end.x), min = std::min(coor_end.x, coor.x);
            for (int p = min + 1; p < max; p++) {
                if (pieces[gameMap[p][coor.y]]->isLife())return false;
            }
            if (!pieces[gameMap[coor_end]]->isLife() || pieces[gameMap[coor_end]]->getColor() != color) {
                pieces[gameMap[coor_end]]->setLife(false);
                updateCoor(gameMap, coor_end);
                return true;
            }
        }
        //横向路径上是否存在棋子的判断
        if (!det.x) {
            int max = std::max(coor.y, coor_end.y), min = std::min(coor_end.y, coor.y);
            for (int p = min + 1; p < max; p++) {
                if (pieces[gameMap[coor.x][p]]->isLife())return false;
            }
            if (!pieces[gameMap[coor_end]]->isLife() || pieces[gameMap[coor_end]]->getColor() != color) {
                pieces[gameMap[coor_end]]->setLife(false);
                updateCoor(gameMap, coor_end);
                return true;

            }
        }
    }
    return false;
}

void Piece_Car::init() {
    switch (pt) {
        case PieceType::RED_CAR1:
            coor.x = 9;
            coor.y = 0;
            color = RED;
            break;
        case PieceType::RED_CAR2:
            coor.x = 9;
            coor.y = 8;
            color = RED;
            break;
        case PieceType::BLACK_CAR1:
            coor.x = 0;
            coor.y = 0;
            color = BLACK;
            break;
        case PieceType::BLACK_CAR2:
            coor.x = 0;
            coor.y = 8;
            color = BLACK;
            break;
        default:
            break;
    }
    life = true;
}

Piece_Car::~Piece_Car() {}
